package file;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.Database;
import model.Table;

/**
 *
 * @author Merli András-Bertalan
 */
public class FileHandler {

    private final static String FILE_PATH = "src/file/databases.txt";
    private final static String TEMPORAL_FILE_PATH = "src/file/";
    private final static Gson gson = new Gson();

    public static void saveTemporalTable(Table table, String fileName) {
        try (PrintWriter out = new PrintWriter(TEMPORAL_FILE_PATH + fileName)) {
            String tableJson = gson.toJson(table);
            out.println(tableJson);
            out.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(FileHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static Table loadTemporalTable(String fileName) {
        try {
            byte[] encoded = Files.readAllBytes(Paths.get(TEMPORAL_FILE_PATH + fileName));
            String json = new String(encoded, StandardCharsets.UTF_8);
            Table table = gson.fromJson(json, new TypeToken<Table>() {
            }.getType());
            return table;
        } catch (NoSuchFileException ex) {
            System.out.println("NoSuchFileException: " + ex.getMessage());
        } catch (IOException ex) {
            Logger.getLogger(FileHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public static void deleteTemporalTable(String fileName) {
        try {
            Files.delete(Paths.get(TEMPORAL_FILE_PATH + fileName));
        } catch (NoSuchFileException ex) {
            System.out.println("NoSuchFileException: " + ex.getMessage());
        } catch (IOException ex) {
            Logger.getLogger(FileHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static List<Database> loadDatabases() {
        try {
            byte[] encoded = Files.readAllBytes(Paths.get(FILE_PATH));
            String json = new String(encoded, StandardCharsets.UTF_8);
            List<Database> databases = gson.fromJson(json, new TypeToken<List<Database>>() {
            }.getType());
            if (databases == null) {
                return new LinkedList<>();
            }
            return databases;
        } catch (IOException ex) {
            Logger.getLogger(FileHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return Collections.<Database>emptyList();
    }

    public static void saveDatabases(final List<Database> databases) {
        try (PrintWriter out = new PrintWriter(FILE_PATH)) {
            String databasesJson = gson.toJson(databases);
            out.println(databasesJson);
            out.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(FileHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
